package cn.ssdcc.dkserver.biz;

import cn.ssdcc.dkserver.entity.DkPath;
import cn.ssdcc.dkserver.entity.Music;
import cn.ssdcc.dkserver.entity.MusicGroup;
import cn.ssdcc.dkserver.exception.BusinessException;
import cn.ssdcc.dkserver.mapper.MusicMapper;
import cn.ssdcc.dkserver.util.DownloadUtil;
import cn.ssdcc.dkserver.util.IsNullUtil;
import cn.ssdcc.dkserver.util.MusicToolUtil;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class MusicToolBiz extends CommonBiz {

    @PostConstruct
    public void init() {
        try {
            File dir = new File(DkPath.DataPath+"/music");
            if(!dir.exists()){
                dir.mkdirs();//创建目录
                log.info("文件夹"+DkPath.DataPath+"/music"+"创建成功");
            }
        } catch (Exception e) {
            log.error("文件夹"+DkPath.DataPath+"/music"+"创建失败",e);
        }
        try {
            File dir = new File(DkPath.DataPath+"/musicimg");
            if(!dir.exists()){
                dir.mkdirs();//创建目录
                log.info("文件夹"+DkPath.DataPath+"/musicimg"+"创建成功");
            }
        } catch (Exception e) {
            log.error("文件夹"+DkPath.DataPath+"/musicimg"+"创建失败",e);
        }
        List<MusicGroup> musicGroups = musicMapper.selectMusicGroup();
        if (IsNullUtil.IsNullList(musicGroups)){
            MusicGroup musicGroup = new MusicGroup();
            musicGroup.setAbout("这个是同步听歌没歌的时候随机的歌单");
            musicGroup.setName("系统随机默认歌单");
            musicGroup.setImg("https://i.postimg.cc/NFg5BxYm/1024x1024bb.png");
            musicGroup.setUuid(UUID.randomUUID().toString());
            musicGroup.setUserid("1");
            musicMapper.inserteMusicGroup(musicGroup);
        }
    }
    public Music downloadMusicByUrl(Music music){
        String[] types = music.getUrl().split("\\.");
        music.setType(types[types.length-1]);
        String path = buildMusicPath("/music/",music.getUrl(),music);
        downloadUtil.downloadNet(music.getUrl(), DkPath.DataPath+path);
        music.setFile(path);
        String imgpath = buildMusicPath("/musicimg/",music.getImg(),music);
        downloadUtil.downloadNet(music.getImg(), DkPath.DataPath+imgpath);
        music.setImg(imgpath);
        music.setUuid(UUID.randomUUID().toString());
        musicMapper.insertMusic(music);
        return music;
    }

    public String buildMusicPath(String dirPath,String url ,Music music){
        String[] types = url.split("\\.");
        String path = dirPath+downloadUtil.clearPath(music.getArname()+"-"+music.getName()+"-"+music.getAlname()+"."+types[types.length-1]);
        return path;
    }

    public void UpMusicByUrlChack(Music music){
        if (IsNullUtil.IsNullString(music.getThirdid())){
            throw new BusinessException("三方ID为空");
        }
        if (IsNullUtil.IsNullString(music.getName())){
            throw new BusinessException("歌曲名称为空");
        }
        if (IsNullUtil.IsNullString(music.getAlname())){
            throw new BusinessException("歌曲专辑为空");
        }
        if (IsNullUtil.IsNullString(music.getArname())){
            throw new BusinessException("歌曲歌手为空");
        }
        if (IsNullUtil.IsNullString(music.getUrl())){
            throw new BusinessException("歌曲URL为空");
        }
        if (IsNullUtil.IsNullString(music.getImg())){
            throw new BusinessException("歌曲图片为空");
        }
    }

    public void SetMusicChack(Music music){
        if (IsNullUtil.IsNullString(music.getName())){
            throw new BusinessException("歌曲名称为空");
        }
        if (IsNullUtil.IsNullString(music.getAlname())){
            throw new BusinessException("歌曲专辑为空");
        }
        if (IsNullUtil.IsNullString(music.getArname())){
            throw new BusinessException("歌曲歌手为空");
        }
        if (IsNullUtil.IsNullString(music.getFile())){
            throw new BusinessException("歌曲文件为空");
        }
        if (IsNullUtil.IsNullString(music.getImg())){
            throw new BusinessException("歌曲图片为空");
        }
        if (IsNullUtil.IsNullString(music.getType())){
            throw new BusinessException("歌曲类型为空");
        }

    }
}
